package com.foxzzz.two.pointers;

public class SolutionV1721 {

    public int trap(int[] height) {
        int ans = 0, left = 0, maxLeft = 0, maxRight = 0, right = height.length - 1;
        while (left <= right) {
            if (maxLeft < maxRight) {
                maxLeft = Math.max(maxLeft, height[left]);
                ans += maxLeft - height[left];
                left++;
            } else {
                maxRight = Math.max(maxRight, height[right]);
                ans += maxRight - height[right];
                right--;
            }
        }
        return ans;
    }


    public static void main(String[] args) {
        System.out.println(new SolutionV1721().trap(new int[]{0, 1, 0, 2, 1, 0, 1, 3, 2, 1, 2, 1}));
    }
}
